﻿using AutoMapper;
using BBICMS.Application.InputModels;
using BBICMS.Application.ViewModels;
using BBICMS.Infrastructure.Configs;
using BBICMS.Model;
using BBICMS.Resources;
using BBICMS.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    public class LanguageController : BaseController
    {
        // GET: Admin/Language
        public ActionResult Index(int? page)
        {

            int PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize;

            LanguageListVM LanguageList = new LanguageListVM
            {
                Languages = _unit.Languages.GetAll().OrderByDescending(s => s.Importance).ThenByDescending(t => t.AddedDate),
                PageSize = 15, //SettingsManager.Site.PageSize,
                PageNumber = page ?? 1
            };      

            return View(LanguageList);
        }

        [HttpGet]
        public ActionResult Add()
        {
            LanguageIM vLanguage = new LanguageIM();
            vLanguage.Active = true;
            vLanguage.Importance = 0;

            return View("Add", vLanguage);
        }



        [HttpPost]     
        public JsonResult Add(LanguageIM languageIM)
        {
            if (ModelState.IsValid)
            {
                var vlanguage = Mapper.Map<Language>(languageIM);
                _unit.Languages.Add(vlanguage);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Language));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Language));
            return Json(AR, JsonRequestBehavior.DenyGet);           

        }


        public ActionResult Edit(int Id)
        {
           
            var vLanguage = _unit.Languages.GetById(Id);
            if (vLanguage != null)
            {
                var language = Mapper.Map<LanguageIM>(vLanguage);
                return View(language);
            }
            return View("NotFound");
        }

        [HttpPost]
        [ValidateInput(false)]

        public JsonResult Edit(LanguageIM languageIM)
        {
            
            if (ModelState.IsValid)
            {
                var language = Mapper.Map<Language>(languageIM);
                language.UpdatedDate = DateTime.Now;
             //   language.UpdatedBy = User.Identity.Name;


                _unit.Languages.Update(language);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Language));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Language));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }


        [HttpPost]
        public JsonResult IsActive(int id)
        {
            var language = _unit.Languages.GetById(id);
            if (language != null)
            {
                language.Active = !language.Active;
                _unit.Languages.Update(language);
                _unit.SaveChanges();

                AR.SetSuccess("设置成功");
                return Json(AR, JsonRequestBehavior.DenyGet);

            }
            AR.Setfailure("设置失败");
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        [HttpPost]
        public JsonResult IsDefault(int id)
        {
            var language = _unit.Languages.GetById(id);
            if (language != null)
            {
                if (language.IsDefault)
                {
                    language.IsDefault = false;
                    _unit.Languages.Update(language);
                }
                else
                {

                    var currentLanguage = _unit.Languages.GetFirstOrDefault(l => l.IsDefault);
                    if (currentLanguage != null)
                    {
                        currentLanguage.IsDefault = false;
                    }

                    language.IsDefault = true;
                    _unit.Languages.Update(language);
                }              

              
                _unit.SaveChanges();

                AR.SetSuccess("设置成功");
                return Json(AR, JsonRequestBehavior.DenyGet);

            }
            AR.Setfailure("设置失败");
            return Json(AR, JsonRequestBehavior.DenyGet);

        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Delete(int id)
        {

            Language vLanguage = _unit.Languages.GetById(id);

            if (vLanguage != null)
            {
               
                _unit.Languages.Delete(vLanguage);
                _unit.SaveChanges();
                AR.SetSuccess(string.Format(Messages.AlertDeleteSuccess, EntityNames.Language));
                return Json(AR, JsonRequestBehavior.DenyGet);

            }
            AR.Setfailure(string.Format(Messages.AlertDeleteFailure, EntityNames.Language));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }
    }
}
